
#include "rotationitem.h"
#include "../VPicture.h"



RotationItem::RotationItem(int Id, float fWidth, float fHeight, CVector& fPosition, float fAngle)
////    m_enReflect(true)
{
    m_fWidth     = fWidth,
    m_fHeight    = fHeight,

    m_v3Position = fPosition;
}

RotationItem::RotationItem(void) :
    m_pic(NULL),
    m_bClick(false),
    //    m_enReflect(true),
    m_bIsPlaying(false)
{
}

RotationItem::RotationItem(VPicture* pic)
{
    RotationItem();
    m_pic = pic;
}
RotationItem::~RotationItem(void)
{
    SAFE_RELEASE(m_pic);
}



bool RotationItem::render(void) const
{
    if (!VNRWidget::render())
    {
        return false;
    }

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -3.0f);
    float  pfVertices[] =
    {
        -m_fWidth / 2, m_fHeight, 0.0f,
        m_fWidth / 2, m_fHeight, 0.0f,
        m_fWidth / 2, 0, 0.0f
    };

    float  pfVertices1[] =
    {
        m_fWidth / 2, 0, 0.0f,
        -m_fWidth / 2, 0, 0.0f,
        -m_fWidth / 2, m_fHeight, 0.0f
    };

    GLfloat textures[] =
    {
        0.0f, 0.0f,
        1.0f, 0.0f,
        1.0f, 1.0f
    };

    GLfloat textures1[] =
    {
        1.0f, 1.0f,
        0.0f, 1.0f,
        0.0f, 0.0f

    };


    glPushMatrix();
    glRotatef(m_fAngle, 0.0, 1.0, 0.0);
    glTranslatef(m_v3Position.x, m_v3Position.y, m_v3Position.z);

    //  if (m_bClick)
    //  {
    //      //画点中状态
    // //       glScalef(m_v3Position.x-10,m_v3Position.y-10, m_v3Position.z);
    //      m_bClick = false;
    //  }
    //  glGetFloatv(GL_MODELVIEW_MATRIX, m_ModelMatrix);
    //  glGetFloatv(GL_PROJECTION_MATRIX, m_ProjMatrix);
    //  glGetIntegerv(GL_VIEWPORT, m_viewport);
    //  int x,y,z;
    //  gluProject(0, 0, 0,m_ModelMatrix,m_ProjMatrix,m_viewport,&x,&y,&z);
    //  m_rc.left   = x-100;
    //  m_rc.right  = x+100;
    //  m_rc.top    = y-50;
    //  m_rc.bottom = y+200;

    glBindTexture(GL_TEXTURE_2D, m_pic->getTexture());

    glTexCoordPointer(2, GL_FLOAT, 0, textures);//右上三角
    glVertexPointer(3, GL_FLOAT, 0, pfVertices);
    glDrawArrays(GL_TRIANGLES, 0, 3);

    glTexCoordPointer(2, GL_FLOAT, 0, textures1);//左下三角
    glVertexPointer(3, GL_FLOAT, 0,  pfVertices1);
    glDrawArrays(GL_TRIANGLES, 0, 3);
#if 0
    //    if (m_enReflect)//画倒影
    //    {
    float  pfVertices2[] =
    {
        -m_fWidth / 2, 0, 0.0f,
        m_fWidth / 2, 0, 0.0f,
        m_fWidth / 2, -m_fHeight* .8 , 0.0f
    };

    float  pfVertices3[] =
    {
        -m_fWidth / 2, -m_fHeight* .8 , 0.0f,
        m_fWidth / 2, -m_fHeight* .8 , 0.0f,
        -m_fWidth / 2, 0, 0.0f
    };
    GLfloat textures2[] =
    {
        0.0f, 1.0f,
        1.0f, 1.0f,
        1.0f, 0.0f
    };
    GLfloat textures3[] =
    {
        0.0f, 0.0f,
        1.0f, 0.0f,
        0.0f, 1.0f
    };

    GLfloat color2[] =
    {
        0.8f, 0.8f, 0.8f,
        0.8f, 0.8f, 0.8f,
        0.0f, 0.0f, 0.0f
    };

    GLfloat color3[] =
    {
        0.0f, 0.0f, 0.0f,
        0.0f, 0.0f, 0.0f,
        0.8f, 0.8f, 0.8f
    };
    glEnableClientState(GL_COLOR_ARRAY);

    glColorPointer(3, GL_FLOAT, 0, color2); //右上三角
    glTexCoordPointer(2, GL_FLOAT, 0, textures2);
    glVertexPointer(3, GL_FLOAT, 0, pfVertices2);
    glDrawArrays(GL_TRIANGLES, 0, 3);

    glColorPointer(3, GL_FLOAT, 0, color3); //左下三角
    glTexCoordPointer(2, GL_FLOAT, 0, textures3);
    glVertexPointer(3, GL_FLOAT, 0,  pfVertices3);
    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableClientState(GL_COLOR_ARRAY);
    //   }

#endif
    glPopMatrix();
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);

    return true;
}

int RotationItem::Click(int x, int y)
{
    POINT pt;
    pt.x = x;
    pt.y = y;

    if (m_bIsPlaying)//播放动画时禁止拣选
    {
        return -1;
    }

    // TODO
    return 0;
}

// void RotationItem::SetV3Position( CVector& fPosition )
// {
//  m_v3Position = fPosition;
// }
//
// CVector & RotationItem::GetSetV3Position()
// {
//  return m_v3Position;
// }
//
//
//
// float RotationItem::GetAngle()
// {
//  return m_fAnglebk;
// }

